<template>
    <el-table
      :data="tableData"
      border
      style="width: 90%; margin: 0px auto;border-radius: 0px; margin: 0px auto;box-shadow: 5px 5px 10px rgba(64, 158, 255, 0.5);"
      :header-cell-style="{
          'background': '#337ecc !important',
          'color': '#ffffff',
          'border': 'none !important'
        }"
		:span-method="(value) => rowSpanRulesHandler(value, tableData)">
    	<el-table-column prop="position" label="岗位" width=""></el-table-column>
    		<el-table-column prop="basicWage" label="基本工资" width=""></el-table-column>
    		<el-table-column prop="scoreWage" label="评分结算" width=""></el-table-column>
    		<el-table-column prop="pieceWage" label="计件结算" width=""></el-table-column>
    		<el-table-column prop="kpiWage" label="提成结算" width=""></el-table-column>
    		<el-table-column prop="okrWage" label="okr结算" width=""></el-table-column>
    		<el-table-column prop="rewardWage" label="特殊绩效" width=""></el-table-column>
    		<el-table-column prop="partTimeRewardWage" label="兼职绩效" width=""></el-table-column>
    		<el-table-column prop="total" label="总计" width=""></el-table-column>
    </el-table>
      
</template>

<script setup>
import {ref, onBeforeMount, reactive,getCurrentInstance,watch} from "vue";


let props = defineProps({
  wageList: {
    type: Array,
    required: true, //是否规定必须得有
  }
})

const tableData=ref(props.wageList)
watch(props, (newValue, oldValue) => {
  tableData.value = props.wageList
},{ deep: true });  




const compareSingleAttrReturnColSpan = (row, compareAttr, index, tableData) => {
  let count = 0;
    let start = index;
  
    while (start < tableData.length) {
    	
    		
    		// 进入循环，检查下一个元素
    		if (start > index && row[compareAttr] !== tableData[start][compareAttr]) {
    			break;  // 如果不相等，则停止合并
    		}
    		count++;  // 计数相同的行
    		start++;  // 移动到下一行
    	
    	
    }
  
    // 将合并的行数存储在行数据中
    if (start > index) {
      tableData[index][`${compareAttr}_count`] = count;
    }
  
    // 检查前一行的合并数量与当前行是否相等
    const previousRowCount = index > 0 ? tableData[index - 1][`${compareAttr}_count`] : 0;
    return previousRowCount === count + 1 ? [0, 0] : [count, 1];
};

const columns = [
  "position",
  "basicWage",
  "scoreWage",
  "pieceWage",
  "kpiWage",
  "okrWage",
  "rewardWage",
  "partTimeRewardWage",
  "total"
];
const rowSpanRulesHandler = ({  row, column, rowIndex, columnIndex }, data) => {
  //console.log(columns[columnIndex]);
 // 根据列名判断进行合并
   if (columns[columnIndex] === "basicWage") {
     return compareSingleAttrReturnColSpan(row, "basicWage", rowIndex, data);
   } 
};


</script>

<style lang="scss" scoped>

.formWrapper{
    width: 100%;
    border: 2px solid #DCDFE6;
    margin: 20px 0;
    border-radius: 5px;
    .formTitle{
        height: 50px;
        font-weight: bold;
        color: #303133;
        font-size: 25px;
        text-align: center;
        line-height: 50px;
        border-bottom: #DCDFE6 1px solid;
    }

    .form{
        width: 1200px;
        margin: 20px auto;
        border-radius: 5px;
        .title {
            display: flex;
            flex-wrap: wrap;
            justify-content: flex-start;/* 替代space-between布局方式 */
            .title_box{
                width: 200px;
                flex: 1;
                height: 50px;
                background-color: #337ECC;
                text-align: center;
                color: #fff;
                line-height: 50px;
                font-size: 20px;
            }
        }
        .centent {
            display: flex;
            flex-wrap: wrap;
            justify-content: flex-start;/* 替代space-between布局方式 */
            .centent_box{
                width: 200px;
                flex: 1;
                height: 50px;
                background-color: #79bbff;
                color: #fff;
                padding: 5px;
                line-height: 40px;
                text-align: center;
            }
        }
    }
    .button{
        width: 200px;
        height: 40px;
        margin: 10px auto;
        float: right;
    }
}
</style>
